package com.Web.servlet;

import java.util.List;
import java.util.Scanner;

public class homework {

    // 登录
    public static boolean login(String username,String password) {
        List<User> list = JDBCUtils.query(User.class, "select * from user where username = ? and password = ?",username
                ,password);
        if (list.size() > 0){
            return true;
        }
        return false;
    }
    // 判断用户是否有权限
    public static String search_permission(String username,String password){
        List<User> list = JDBCUtils.query(User.class,
                "select * from user where username = ? and password = ?",
                username,password);
        User user1 = new User();
        for (User user:list) {
            user1 = user;
        }
        String is_admin = user1.getisIs_admin();
        return is_admin;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的用户名:");
        String username = scanner.next();
        System.out.println("请输入您的密码:");
        String password = scanner.next();
        if (login(username,password) == true) {
            if (search_permission(username,password).equals("true")) {
                System.out.println("");
                while (true) {
                    System.out.println("-------------------------------------");
                    System.out.println(" 1) 查看宠物");
                    System.out.println(" 2) 添加宠物");
                    System.out.println(" 3) 删除宠物");
                    System.out.println(" 4) 更新宠物");
                    System.out.println(" 5) 退出");
                    System.out.println("-------------------------------------");
                    System.out.println(" 请选择要进行的操作:");
                    Scanner sc = new Scanner(System.in);
                    int choose = sc.nextInt();
                    if (choose == 1) {
                        System.out.println("请输入排序方式:");
                        System.out.println(" 1. 按年龄");
                        System.out.println(" 2. 按价格");
                        int choose1 = sc.nextInt();
                        if (choose1 == 1) {
                            show_AccordAge();
                        }
                        if (choose1 == 2) {
                            show_AccordPrice();
                        }
                    }

                    if (choose == 2) {
                        System.out.println("主人id:");
                        int host_id = sc.nextInt();
                        System.out.println("昵称:");
                        String name = sc.next();
                        System.out.println("类型:");
                        String type = sc.next();
                        System.out.println("年龄:");
                        int age = sc.nextInt();
                        System.out.println("性别:");
                        String gender = sc.next();
                        System.out.println("价格:");
                        int price = sc.nextInt();
                        JDBCUtils.update("insert into pet(host_id,name,type,age,gender,price) values(?,?,?,?,?,?)",
                                host_id,name,type,age,gender,price);
                        System.out.println("更新成功！");
                    }
                    if (choose == 3) {
                        System.out.println("请输入要删除宠物的id:");
                        int id = sc.nextInt();
                        JDBCUtils.update("delete from pet where id = ?",id);
                        System.out.println("删除成功！");
                    }
                    if (choose == 4) {
                        System.out.println("请输入要修改的宠物id:");
                        int id = sc.nextInt();
                        System.out.println("该宠物的原有信息:");
                        search_pet(id);
                        System.out.println("该宠物的更新信息");
                        System.out.println("主任id:");
                        int host_id = sc.nextInt();
                        System.out.println("昵称:");
                        String name = sc.next();
                        System.out.println("类型:");
                        String type = sc.next();
                        System.out.println("年龄:");
                        int age = sc.nextInt();
                        System.out.println("性别:");
                        String gender = sc.next();
                        System.out.println("价格:");
                        int price = sc.nextInt();
                        JDBCUtils.update("update pet set host_id = ?,name = ?,type = ?,age = ?,gender = ?,price = ? where id = ?",
                                host_id,name,type,age,gender,price,id);
                    }
                    if (choose == 5) {
                        return;
                    }
                }
            }
            if (search_permission(username,password).equals("false")) {
                while (true) {
                    System.out.println(" 1) 查看宠物");
                    System.out.println(" 2) 购买宠物");
                    System.out.println(" 3) 退出");
                    System.out.println(" 请选择要进行的操作:");
                    Scanner sc = new Scanner(System.in);
                    int choose = sc.nextInt();
                    if (choose == 1) {
                        System.out.println("请输入排序方式:");
                        System.out.println(" 1. 按年龄");
                        System.out.println(" 2. 按价格");
                        int choose1 = sc.nextInt();
                        if (choose1 == 1) {
                            show_AccordAge();
                        }
                        if (choose1 == 2) {
                            show_AccordPrice();
                        }
                    }
                    if (choose == 2) {
                        System.out.println("请输入要购买宠物的id:");
                        int id = sc.nextInt();
                        search_pet(id);
                        if (search_Userpoints(username,password) >= search_petprice(id)){
                            JDBCUtils.update("update pet set host_id = ? where id = ?",search_Userid(username,password),id);
                            System.out.println("购买成功！");
                            search_pet(id);
                            JDBCUtils.update("update user set points = ? where username = ? and password = ?",
                                    search_Userpoints(username,password)-search_petprice(id),username,password);
                        }else{
                            System.out.println("您的积分不足！");
                        }

                    }
                    if (choose == 3) {
                        return;
                    }
                }
            }
        }else{
            System.out.println("密码输入错误！");
        }
    }

    // 查看宠物(按年龄)
    public static void show_AccordAge() {
        List<pet> list = JDBCUtils.query(pet.class, "select * from pet order by age");
        for (pet pet : list) {
            System.out.println(pet);
        }
    }
    // 查看宠物(按价格)
    public static void show_AccordPrice() {
        List<pet> list = JDBCUtils.query(pet.class, "select * from pet order by price");
        for (pet pet : list) {
            System.out.println(pet);
        }
    }
    // 查看用户id
    public static int search_Userid(String username,String password) {
        List<User> list = JDBCUtils.query(User.class, "select * from user where username = ? and password = ?",
                username,password);
        User user1 = new User();
        for (User user:list) {
            user1 = user;
        }
        int id = user1.getId();
        return id;
    }
    // 查看用户积分
    public static int search_Userpoints(String username,String password) {
        List<User> list = JDBCUtils.query(User.class, "select * from user where username = ? and password = ?",
                username,password);
        User user1 = new User();
        for (User user:list) {
            user1 = user;
        }
        int point = user1.getPoints();
        return point;
    }
    // 查看宠物价格
    public static int search_petprice(int id) {
        List<pet> list = JDBCUtils.query(pet.class, "select * from pet where id = ?", id);
        pet pet = new pet();
        for (pet pets:list) {
            pet = pets;
        }
        double price = pet.getPrice();
        return (int) price;
    }
    // 搜索宠物信息
    public static void search_pet(int id){
        List<pet> list = JDBCUtils.query(pet.class,
                "select * from pet where id = ?",id);
        for (pet pets:list){
            System.out.println(pets);
        }
    }
}